<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>js语法 | 凯的个人博客</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="blog">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.1299ef83.css" as="style"><link rel="preload" href="/assets/js/app.ba7d257b.js" as="script"><link rel="preload" href="/assets/js/3.018f6450.js" as="script"><link rel="preload" href="/assets/js/1.7b7c57c8.js" as="script"><link rel="preload" href="/assets/js/17.e2acf9c8.js" as="script"><link rel="prefetch" href="/assets/js/10.fde56a2d.js"><link rel="prefetch" href="/assets/js/11.8c4e5f05.js"><link rel="prefetch" href="/assets/js/12.e3c30c69.js"><link rel="prefetch" href="/assets/js/13.8229d868.js"><link rel="prefetch" href="/assets/js/14.32869bf0.js"><link rel="prefetch" href="/assets/js/15.2263ef4b.js"><link rel="prefetch" href="/assets/js/16.2ab685d2.js"><link rel="prefetch" href="/assets/js/18.7dfc157d.js"><link rel="prefetch" href="/assets/js/19.d1da7e7c.js"><link rel="prefetch" href="/assets/js/20.e23c5514.js"><link rel="prefetch" href="/assets/js/21.9dd252a6.js"><link rel="prefetch" href="/assets/js/22.c0fc1edc.js"><link rel="prefetch" href="/assets/js/23.68b6a1ef.js"><link rel="prefetch" href="/assets/js/24.cdb5a0a0.js"><link rel="prefetch" href="/assets/js/25.4c09c358.js"><link rel="prefetch" href="/assets/js/26.a9096ca6.js"><link rel="prefetch" href="/assets/js/27.13a75237.js"><link rel="prefetch" href="/assets/js/28.33bdb258.js"><link rel="prefetch" href="/assets/js/29.74801f15.js"><link rel="prefetch" href="/assets/js/30.30b419c0.js"><link rel="prefetch" href="/assets/js/31.524f160e.js"><link rel="prefetch" href="/assets/js/32.9c5d3891.js"><link rel="prefetch" href="/assets/js/33.9070a57a.js"><link rel="prefetch" href="/assets/js/34.d27b9d35.js"><link rel="prefetch" href="/assets/js/35.3f94918e.js"><link rel="prefetch" href="/assets/js/36.3ec7537c.js"><link rel="prefetch" href="/assets/js/37.087a17ad.js"><link rel="prefetch" href="/assets/js/38.e79a060b.js"><link rel="prefetch" href="/assets/js/39.219f3fef.js"><link rel="prefetch" href="/assets/js/4.9a7e3e1b.js"><link rel="prefetch" href="/assets/js/40.dc82901c.js"><link rel="prefetch" href="/assets/js/41.e98ec0ac.js"><link rel="prefetch" href="/assets/js/42.96575cb1.js"><link rel="prefetch" href="/assets/js/43.e0bb13d9.js"><link rel="prefetch" href="/assets/js/44.7ee919e9.js"><link rel="prefetch" href="/assets/js/45.65f8d6dd.js"><link rel="prefetch" href="/assets/js/46.70530ec4.js"><link rel="prefetch" href="/assets/js/47.35587fbc.js"><link rel="prefetch" href="/assets/js/48.a6fdae9d.js"><link rel="prefetch" href="/assets/js/49.f369ed81.js"><link rel="prefetch" href="/assets/js/5.87035e91.js"><link rel="prefetch" href="/assets/js/50.519b62e3.js"><link rel="prefetch" href="/assets/js/51.38baa29a.js"><link rel="prefetch" href="/assets/js/6.74d67ffd.js"><link rel="prefetch" href="/assets/js/7.4c822714.js"><link rel="prefetch" href="/assets/js/8.69f56ef7.js"><link rel="prefetch" href="/assets/js/9.dec685f7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1299ef83.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-7dd95ae2><div data-v-7dd95ae2><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>凯的个人博客</h3> <p class="description" data-v-59e6cb88>blog</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>凯</span>
          
        <!---->
        2024
      </a></span></div></div> <div class="hide" data-v-7dd95ae2><header class="navbar" data-v-7dd95ae2><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.jpg" alt="凯的个人博客" class="logo"> <span class="site-name">凯的个人博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端技术
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/react/" class="nav-link"><i class="undefined"></i>
  react
</a></li><li class="dropdown-item"><!----> <a href="/categories/ts/" class="nav-link"><i class="undefined"></i>
  ts
</a></li><li class="dropdown-item"><!----> <a href="/categories/微信小程序/" class="nav-link"><i class="undefined"></i>
  微信小程序
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      消息栏
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      github链接
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-7dd95ae2></div> <aside class="sidebar" data-v-7dd95ae2><div class="personal-info-wrapper" data-v-210250cf data-v-7dd95ae2><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-210250cf> <h3 class="name" data-v-210250cf>
    凯
  </h3> <div class="num" data-v-210250cf><div data-v-210250cf><h3 data-v-210250cf>40</h3> <h6 data-v-210250cf>文章</h6></div> <div data-v-210250cf><h3 data-v-210250cf>6</h3> <h6 data-v-210250cf>分类</h6></div></div> <ul class="social-links" data-v-210250cf></ul> <hr data-v-210250cf></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      前端技术
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/js/" class="nav-link"><i class="undefined"></i>
  js
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/react/" class="nav-link"><i class="undefined"></i>
  react
</a></li><li class="dropdown-item"><!----> <a href="/categories/ts/" class="nav-link"><i class="undefined"></i>
  ts
</a></li><li class="dropdown-item"><!----> <a href="/categories/微信小程序/" class="nav-link"><i class="undefined"></i>
  微信小程序
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      消息栏
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      github链接
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><a href="/blogs/Front/js/01-js基础.html" class="sidebar-link">js基础</a></li><li><a href="/blogs/Front/js/02-js语法.html" class="active sidebar-link">js语法</a></li><li><a href="/blogs/Front/js/03-js数学方法.html" class="sidebar-link">js数学方法</a></li><li><a href="/blogs/Front/js/04-js数组进阶.html" class="sidebar-link">js数组进阶</a></li><li><a href="/blogs/Front/js/05-day.js第三方时间插件.html" class="sidebar-link">day.js第三方时间插件</a></li><li><a href="/blogs/Front/js/06-js事件.html" class="sidebar-link">js事件</a></li><li><a href="/blogs/Front/js/07-事件Dom与Bom.html" class="sidebar-link">事件Dom与Bom</a></li><li><a href="/blogs/Front/js/08-Ajax网络请求.html" class="sidebar-link">Ajax 网络请求</a></li><li><a href="/blogs/Front/js/09-ajax封装与应用.html" class="sidebar-link">ajax 封装与应用</a></li><li><a href="/blogs/Front/js/10-git操作与项目管理.html" class="sidebar-link">git操作与项目管理</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>js语法</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>凯</span>
          
        <!---->
        2024
      </a></span></div></div> <div data-v-7dd95ae2><div data-v-7dd95ae2><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">js语法</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>凯</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>4/25/2024</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>js</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="js基础"><a href="#js基础" class="header-anchor">#</a> JS基础</h1> <ul><li><p>流程控制</p></li> <li><p>循环</p></li> <li><p>对象</p></li> <li><p>函数</p></li> <li><p>数组</p></li></ul> <h2 id="_1-流程控制"><a href="#_1-流程控制" class="header-anchor">#</a> 1.流程控制</h2> <ul><li><p>单分支</p> <blockquote><p>条件：可以是任何运算符的表达式   a&amp;&amp;b a+b  a&gt;=b</p> <p>​			可以是一个变量，甚至是一个函数的调用   a   !a   sum()</p> <p>​			表达式为假：空字符串''、数字0、null、undefined（非空字符串都为真）</p></blockquote></li> <li><p>多分支语句</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">switch</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">0</span><span class="token operator">:</span>
        语句一<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
        语句二<span class="token punctuation">;</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
        语句三<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li> <li><p>三目运算</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <h2 id="_2-流程控制-循环"><a href="#_2-流程控制-循环" class="header-anchor">#</a> 2.流程控制 -- 循环</h2> <ul><li><p>for循环</p> <blockquote><p>定义中间变量 关键字，必须用let</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">20</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//循环体</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></blockquote></li> <li><p>while</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>条件<span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token comment">//循环体</span>
    i<span class="token operator">+=</span><span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>do ···while（无论有没有满足条件都要先执行一次）</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">do</span><span class="token punctuation">{</span>
    <span class="token comment">//</span>
<span class="token punctuation">}</span><span class="token keyword">while</span><span class="token punctuation">(</span>条件<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li></ul> <h2 id="_3-对象"><a href="#_3-对象" class="header-anchor">#</a> 3.对象</h2> <ul><li>定义对象</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//实例化</span>
<span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">object</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">key</span><span class="token operator">:</span>val<span class="token punctuation">,</span><span class="token literal-property property">key1</span><span class="token operator">:</span>val1<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//字面量</span>
<span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">key</span><span class="token operator">:</span>val<span class="token punctuation">,</span><span class="token literal-property property">key1</span><span class="token operator">:</span>val1<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li><p>使用对象</p> <blockquote><p>描述一个单独的个体：对象</p> <p>eg：有个同学叫晶晶，年龄20岁，性别是女生</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'晶晶'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    <span class="token literal-property property">gender</span><span class="token operator">:</span><span class="token string">'女生'</span>
 <span class="token punctuation">}</span>
 doucument<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">' '</span> <span class="token operator">+</span> user<span class="token punctuation">.</span>age <span class="token operator">+</span> <span class="token string">' '</span> <span class="token operator">+</span> user<span class="token punctuation">.</span>gender<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li> <li><p>对象取值</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>对象名<span class="token punctuation">.</span>属性名
对象名<span class="token punctuation">[</span><span class="token string">'属性名'</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_1-对象合并"><a href="#_1-对象合并" class="header-anchor">#</a> 1.对象合并</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//改变原对象的合并</span>
<span class="token keyword">let</span> userInfo <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>userMoney<span class="token punctuation">)</span>   <span class="token comment">//userInfo 的值和user的值是相同的</span>

<span class="token comment">//不改变原对象的合并</span>
<span class="token keyword">let</span> UserInfo <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>User<span class="token punctuation">,</span>user1<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_2-对象遍历"><a href="#_2-对象遍历" class="header-anchor">#</a> 2.对象遍历</h3> <h4 id="_1-for-in循环"><a href="#_1-for-in循环" class="header-anchor">#</a> 1.for```in循环</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
     <span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'晶晶'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    <span class="token literal-property property">gender</span><span class="token operator">:</span><span class="token string">'女生'</span>
 <span class="token punctuation">}</span>
 <span class="token comment">//for in</span>
 <span class="token keyword">for</span><span class="token punctuation">(</span>key <span class="token keyword">in</span> user<span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>user<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h4 id="_2-foreach"><a href="#_2-foreach" class="header-anchor">#</a> 2.forEach</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
     <span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'晶晶'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    <span class="token literal-property property">gender</span><span class="token operator">:</span><span class="token string">'女生'</span>
 <span class="token punctuation">}</span>
 <span class="token comment">//</span>
 <span class="token keyword">let</span> userKeyArr <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
 userKeyArr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>user<span class="token punctuation">[</span>item<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h4 id="_3"><a href="#_3" class="header-anchor">#</a> 3.</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
     <span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'晶晶'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    <span class="token literal-property property">gender</span><span class="token operator">:</span><span class="token string">'女生'</span>
 <span class="token punctuation">}</span>

 <span class="token keyword">let</span> userValueArr <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
 userValueArr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_4-函数"><a href="#_4-函数" class="header-anchor">#</a> 4.函数</h2></li> <li><p>函数的定义方式</p></li> <li><p>函数的参数</p> <ul><li>形参</li> <li>实参</li></ul></li> <li><p>函数的参数默认值的设置</p></li> <li><p>函数的参数获取 arguments</p></li> <li><p>函数的返回值 return</p></li> <li><p>函数的调用方式</p> <ul><li>直接调用</li> <li>对调  对象里面的方法调用</li> <li>回调</li> <li>自调--递归调用</li> <li>实例化调用  new</li></ul></li> <li><p>函数的作用域 作用域链   【面试题】</p></li></ul> <h3 id="_1-函数定义方式"><a href="#_1-函数定义方式" class="header-anchor">#</a> 1.函数定义方式</h3> <ul><li>ES 5</li></ul> <blockquote><p>方式1 通过function关键打头直接定义的函数，具体 函数提升特性：在js运行过程中那，会将这个函数 提升到整个js文件的最前面</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//方式1 </span>
<span class="token keyword">function</span> <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>
<span class="token comment">//方式2：必须遵循 先定义 后使用原则</span>
<span class="token keyword">var</span> <span class="token function-variable function">函数名</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//案例</span>

<span class="token comment">// 调用</span>
    <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span>

    <span class="token comment">// 求和函数</span>

    <span class="token keyword">var</span> <span class="token function-variable function">sum</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>    <span class="token comment">//报错：sum is not a function </span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li><p>ES 6 箭头函数</p> <blockquote><p>形参：函数定义时 ，写到小括号中</p> <p>实参：函数调用时，写到小括号中</p></blockquote></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>   <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span>    <span class="token comment">//实参</span>
    <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   <span class="token comment">//形参</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><p>形参和 实参，需要一 一对应的规则</p> <p>如果没有传递实参，但是有形参：形参默认值设置</p></blockquote> <ul><li>形参默认值设置</li></ul> <blockquote><p>方式1：借助if 判断 赋值默认值</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code>  <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span><span class="token number">500</span><span class="token punctuation">)</span>    <span class="token comment">//实参</span>
  <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">)</span>
    <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   
        <span class="token comment">// 函数形参默认值设置</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>c <span class="token operator">==</span><span class="token keyword">undefined</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            c<span class="token operator">=</span><span class="token number">1000</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b <span class="token operator">+</span> c<span class="token punctuation">)</span>  
    <span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>方式2：ES6 默认值设置语法糖-[推荐]</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code>  <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span><span class="token number">500</span><span class="token punctuation">)</span>    <span class="token comment">//实参</span>
  <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">)</span>
    <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token number">1000</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>   
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b <span class="token operator">+</span> c<span class="token punctuation">)</span>  
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>arguments 形参列表 ES5</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code>   <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span>
    <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">222</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">45</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span>
    <span class="token function">sum</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">312</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4545</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">45</span><span class="token punctuation">,</span><span class="token number">45</span><span class="token punctuation">,</span><span class="token number">45</span><span class="token punctuation">)</span>
    <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span>
        <span class="token comment">// arguments :伪数组、类数组：类似于数组的 假数组</span>
        <span class="token comment">// 伪数组、类数组都不能数组所有方法26</span>
        <span class="token comment">// for循环【推荐】</span>
        <span class="token keyword">let</span> num <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>arguments<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            num <span class="token operator">+=</span> arguments<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>   
    <span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>案例： 实现无数个数字的 乘积</p> <h2 id="_6-函数返回值-return"><a href="#_6-函数返回值-return" class="header-anchor">#</a> 6.函数返回值  return</h2> <blockquote><p>函数一旦调用之后 执行完成后 大多数情况都要有返回值，</p> <p>函数体没有写返回值<code>return</code>     或：<code>return ;</code>  默认返回 unfefined</p> <p>一个函数中如果存在多个<code>return</code>   当函数运行过程中只要遇到了return 就会立即结束函数的运行</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//请求通过函数的封装 实现 传递进入xxx数据和xxx数据，返回得到一个新数据</span>

<span class="token keyword">function</span> <span class="token function">函数名</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span>b</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//创建一个 返回值存储的变量</span>
    <span class="token keyword">let</span> str<span class="token punctuation">;</span> 
    
    <span class="token comment">//运行过程中 计算结果 保存到这变量中</span>
    
    
    <span class="token comment">//返回数据</span>
    <span class="token keyword">return</span> str<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="练习1"><a href="#练习1" class="header-anchor">#</a> 练习1</h2> <p>1.封装一个函数实现 对象转为字符串的功能</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">id</span><span class="token operator">:</span><span class="token number">1000</span><span class="token punctuation">,</span>
    <span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'苏鹏'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">age</span><span class="token operator">:</span><span class="token number">20</span><span class="token punctuation">,</span>
    <span class="token literal-property property">like</span><span class="token operator">:</span><span class="token string">'吃饭睡觉打豆豆'</span>
<span class="token punctuation">}</span>


<span class="token keyword">function</span> <span class="token function">format</span> <span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
       <span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
        <span class="token comment">//obj==&gt;'key=val&amp;key1=val1'  </span>
        <span class="token comment">// Object.keys(obj)  //['id',xxxx,xxx]</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span>key <span class="token keyword">in</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>   <span class="token comment">// key==‘id’  'name'  'age' 'like'</span>
            str <span class="token operator">+=</span> <span class="token string">'&amp;'</span> <span class="token operator">+</span> key <span class="token operator">+</span> <span class="token string">'='</span> <span class="token operator">+</span> obj<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 字符串截取 去掉 第0个&amp; </span>
        str <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> str<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token keyword">let</span> userStr <span class="token operator">=</span>  <span class="token string">'id=1000&amp;name=苏鹏&amp;age=20&amp;like=吃饭睡觉打豆豆'</span>


  
<span class="token comment">//2.任何对象传递过去 都可以拿到一个 类似的结果   'key=val&amp;key1=val1'</span>
    <span class="token keyword">let</span> userStr <span class="token operator">=</span> <span class="token function">format</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>userStr<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h2 id="_7-函数的调用方式"><a href="#_7-函数的调用方式" class="header-anchor">#</a> 7.函数的调用方式</h2> <h3 id="_1-直接调用"><a href="#_1-直接调用" class="header-anchor">#</a> 1.直接调用</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">函数名</span><span class="token punctuation">(</span>实参<span class="token number">1</span>，实参<span class="token number">2.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_2-对调-对象中的方法调用"><a href="#_2-对调-对象中的方法调用" class="header-anchor">#</a> 2.对调   对象中的方法调用</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code>对象<span class="token punctuation">.</span><span class="token function">属性名</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
对象<span class="token punctuation">.</span><span class="token function">方法</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> user <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">name</span><span class="token operator">:</span><span class="token string">'张麻子'</span><span class="token punctuation">,</span>
    <span class="token function-variable function">eat</span><span class="token operator">:</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">food</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">'正在吃'</span><span class="token operator">+</span>food
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">//调用</span>
user<span class="token punctuation">.</span><span class="token function">eat</span><span class="token punctuation">(</span><span class="token string">'西瓜'</span><span class="token punctuation">)</span>  <span class="token comment">//正在吃西瓜</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_3-回调"><a href="#_3-回调" class="header-anchor">#</a> 3.回调</h3> <blockquote><p>在函数fn调用的过程中，传递的实参 本身又是一个函数callFn，</p> <p>在函数fn执行过程中，又调用了 callFn这个回调函数</p></blockquote> <blockquote><p>回调：现在不调（调用大函数的时候）   回头再调：在大函数运行过程中 调用这个传递的实参函数</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//调用一个函数，等待2s以后 实现页面打开弹窗 alert()</span>
<span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token parameter">callFn</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//有很多的业务逻辑</span>
    <span class="token comment">//回调 </span>
   <span class="token function">callFn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">fn</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
        <span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">2000</span><span class="token punctuation">)</span>  
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1222</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">]</span>

<span class="token comment">//forEach 中传递的function就是一个回调函数 --同步代码 --异步代码【全部需要使用回调】 </span>
arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_4-自调-递归调用"><a href="#_4-自调-递归调用" class="header-anchor">#</a> 4.自调--递归调用</h3> <blockquote><p>在函数体的内部 调用 自己</p> <p>一定会有退出的条件，递归的出口  ：（没有出口 必定造成死循环）</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code> <span class="token keyword">function</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token parameter">num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 递归的出口 循环终止的条件</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>num<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>  <span class="token comment">//最后一次循环需要返回的数据</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> num <span class="token operator">*</span> <span class="token function">sum</span><span class="token punctuation">(</span>num <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//10*9*8*....*2* 1 ==1</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">let</span> xx <span class="token operator">=</span> <span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_5-实例化调用"><a href="#_5-实例化调用" class="header-anchor">#</a> 5.实例化调用</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_8-内置对象的方法"><a href="#_8-内置对象的方法" class="header-anchor">#</a> 8.内置对象的方法</h2> <ul><li>字符串 有哪些内置方法</li></ul> <h3 id="indexof"><a href="#indexof" class="header-anchor">#</a> indexOf</h3> <blockquote><p>作用： 查找字符串中 能够匹配当前字符串的字符串位置，返回值是 查找到的位置索引 ，如果没有找到 返回-1</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">'你好啊毛秋菊同学'</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'同学'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 6</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'徐练'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//-1</span>



<span class="token comment">// 小==》大 </span>

<span class="token keyword">let</span> str1 <span class="token operator">=</span> <span class="token string">'asdasdsadasAAAAAd'</span><span class="token punctuation">;</span>   
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str1<span class="token punctuation">.</span><span class="token function">toLocaleUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//ASDASDSADASAAAAAD</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str1<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//ASDASDSADASAAAAAD</span>

<span class="token comment">//大==》小</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str1<span class="token punctuation">.</span><span class="token function">toLocaleLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//asdasdsadasaaaaad</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str1<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//asdasdsadasaaaaad</span>

<span class="token comment">// 字符串截取</span>
<span class="token keyword">let</span> str2 <span class="token operator">=</span> <span class="token string">'你好啊毛秋菊同学'</span>
<span class="token comment">// substring(起始索引,终点索引)      substr(起始的索引，截取的长度) </span>
<span class="token comment">// slice(起始索引,终点索引)  索引可以是负数 -1：倒数第一个</span>
<span class="token comment">// 起始索引对应的字符 截取时包含在内 ， 终点索引不包含在内</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span> str2<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//毛秋菊</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str2<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//毛秋菊</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str2<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//毛秋菊</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str2<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//毛秋菊</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><ul><li>数学 有哪些内置方法 Math</li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">5/27/2024, 8:38:35 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blogs/Front/js/01-js基础.html" class="prev">
          js基础
        </a></span> <span class="next"><a href="/blogs/Front/js/03-js数学方法.html">
          js数学方法
        </a></span></p></div> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:0;" data-v-b57cc07c data-v-7dd95ae2></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-6e9c9188 data-v-6e9c9188><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-6e9c9188><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-6e9c9188></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-6e9c9188></path></svg></div><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.ba7d257b.js" defer></script><script src="/assets/js/3.018f6450.js" defer></script><script src="/assets/js/1.7b7c57c8.js" defer></script><script src="/assets/js/17.e2acf9c8.js" defer></script>
  </body>
</html>
